<template>
  <el-drawer
    :visible.sync="visible"
    :with-header="false"
    size="500px"
    :destroy-on-close="true"
    @closed="handleClose"
  >
    <div class="drawer-container">
      <div class="drawer-header">
        <div class="header-content">
          <span class="title">禁用分类设置</span>
          <div class="warning-tip">提示：禁用分类后，该分类下国内贸易类型的商品不久后将会被自动清除</div>
        </div>
        <el-button type="primary" size="small" @click="handleSave">保存</el-button>
      </div>
      <div class="drawer-content">
        <el-tree
          ref="tree"
          :data="treeData"
          show-checkbox
          node-key="id"
          :props="defaultProps"
          :default-checked-keys="checkedKeys"
          @check="handleCheck"
        />
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { getCategory, setDisableCategories, getDisableCategories } from '@/api/externalGoods'

export default {
  name: 'DisableCategoryDrawer',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      checkedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getTreeData()
        this.getDisabledCategories()
      }
    }
  },
  methods: {
    async getTreeData() {
      try {
        const res = await getCategory()
        if (res.code === 10000) {
          this.treeData = res.data
        }
      } catch (error) {
        console.error('获取分类数据失败:', error)
      }
    },
    async getDisabledCategories() {
      try {
        const res = await getDisableCategories()
        if (res.code === 10000) {
          this.checkedKeys = res.data
        }
      } catch (error) {
        console.error('获取禁用分类失败:', error)
      }
    },
    handleCheck(data, checked) {
      const { checkedKeys } = checked
      this.checkedKeys = checkedKeys
    },
    async handleSave() {
      try {
        // 获取选中的节点ID
        const checkedNodes = this.$refs.tree.getCheckedNodes()
        const checkedIds = checkedNodes.map(node => node.id)
        
        // 保存到后端
        const res = await setDisableCategories({ category_ids: checkedIds })
        if (res.code === 10000) {
          this.$message.success('保存成功')
          this.$emit('update:visible', false)
        } else {
          this.$message.error(res.message || '保存失败')
        }
      } catch (error) {
        console.error('保存禁用分类失败:', error)
        this.$message.error('保存失败')
      }
    },
    handleClose() {
      // 重置数据
      this.treeData = []
      this.checkedKeys = []
      // 通知父组件更新visible状态
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.drawer-container {
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  
  .drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 20px;
    
    .header-content {
      .title {
        font-size: 18px;
        font-weight: bold;
        display: block;
        margin-bottom: 8px;
      }
      
      .warning-tip {
        font-size: 12px;
        color: #E6A23C;
      }
    }
  }
  
  .drawer-content {
    flex: 1;
    overflow-y: auto;
  }
}
</style> 